﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Configuration;
using System.Data.Common;
using System.Threading;
using JsalesModel;

namespace JSales
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
            //log4net
            //log4net.Config.XmlConfigurator.Configure();
            //nhProfiler
        }

        private void Button2Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Button1Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(textPass.Text))
            {
                MessageBox.Show("密码不能为空");
            }
            else
            {
                using (var dbcontext= new JYEntities())
                {
                    TbUser user =
                        (from u in dbcontext.TbUsers
                         where u.UserID == Convert.ToInt32(comboUsers.SelectedValue)
                         select u).FirstOrDefault();
                    GlobalVar.cUser = user;
                }

                if (GlobalVar.cUser.PSWD.Equals(textPass.Text))
                {
//                    toolStripProgressBar1.Visible = true;
//                    toolStripStatusLabel1.Visible = true;
//                    toolStripProgressBar1.Maximum = 220;
//
                    GlobalVar.RegRole();
                    GlobalVar.RegModel();
                    GlobalVar.SelectDept();
//                    toolStripProgressBar1.Value = 10;
//                    toolStripStatusLabel1.Text = "加载部门信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
//                    GlobalVar.SelectRole();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载角色信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
//                    GlobalVar.SelectUsers();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载用户信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
//                    GlobalVar.SelectRight();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载权限信息";
//                    this.Refresh();
//                    Thread.Sleep(1);

                   GlobalVar.SelectCusLevel();
//                  toolStripProgressBar1.Value += 10;
//                  toolStripStatusLabel1.Text = "加载级别信息";
//                  this.Refresh();
//                  Thread.Sleep(1);;
                    
                                        
                   GlobalVar.SelectCustomer();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载客户信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
                   GlobalVar.SelectProductRaw(); 
                   GlobalVar.SelectProductSemi(); 
                   GlobalVar.SelectProduct();
                   GlobalVar.SelectProductAll();

                    GlobalVar.InitTypes();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载产品信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
                    GlobalVar.SelectKind();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载类型信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
                    GlobalVar.SelectPrice();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载价格信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
//                    GlobalVar.SelectDegree();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载程度信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
                    GlobalVar.SelectColor();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载颜色信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
//                    GlobalVar.SelectGrammage();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载克重信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
                    GlobalVar.SelectTexture();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载纹路信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
//                    GlobalVar.SelectProcess();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载工艺信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
//                    GlobalVar.SelectLevel();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载等级信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
//                    GlobalVar.SelectStore();
//                    toolStripProgressBar1.Value += 10;
//                    toolStripStatusLabel1.Text = "加载仓库信息";
//                    this.Refresh();
//                    Thread.Sleep(1);;
//
//                    //窗口状态
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("密码错误！");
                }
            }
        }

        private void LoginFormLoad(object sender, EventArgs e)
        {
            GlobalVar.SelectUsers();
            IQueryable<TbUser> loginUser = from i in GlobalVar.GUsers
                                         where i.IsDeleted != true
                                         select i;
            comboUsers.DataSource = loginUser.ToList();
            comboUsers.ValueMember = "UserID";
            comboUsers.DisplayMember = "UserName";
        }
    }
}
